<div class="panel panel-default panel-intro">
    {:build_heading()}

    <style>
        .spec-btn{
            margin-right: 10px;
        }
    </style>

    <div class="panel-body">
        <form id="add-form" class="form-horizontal add-form" role="form" data-toggle="validator" method="POST" action="">
            <!-- Nav tabs -->
            <ul class="nav nav-tabs" role="tablist">
                <li role="presentation" class="active"><a href="#info" aria-controls="info" role="tab" data-toggle="tab">商品信息</a></li>
                <li role="presentation"><a href="#imgs" aria-controls="imgs" role="tab" data-toggle="tab">商品相册</a></li>
                <li role="presentation"><a href="#spec" aria-controls="spec" role="tab" data-toggle="tab">商品规格</a></li>
                <li role="presentation"><a href="#attribute" aria-controls="attribute" role="tab" data-toggle="tab">商品属性</a></li>
            </ul>

            <!-- Tab panes -->
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="info">
                    <div class="form-group"></div>
                    <div class="form-group">
                        <label for="c-category_id" class="control-label col-xs-12 col-sm-2">{:__('Category_id')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <select name="row[category_id]" data-rule="required" id="c-category_id" class="form-control" v-model="category_id">
                                <option value="">分类</option>
                                {foreach name="categorys" item="vo"}
                                    <option value="{$vo.id}" >{$vo.name}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <!--  
                        <div class="form-group">
                            <label for="c-extend_cat_id" class="control-label col-xs-12 col-sm-2">{:__('Extend_cat_id')}:</label>
                            <div class="col-xs-12 col-sm-8">
                                
                                <select name="row[extend_cat_id]" v-model="row.extend_cat_id" id="c-extend_cat_id" class="form-control">
                                    <option value="">扩展分类</option>
                                    {foreach name="categorys" item="vo"}
                                        <option value="{$vo.id}">{$vo.name}</option>
                                    {/foreach}
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="c-goods_no" class="control-label col-xs-12 col-sm-2">{:__('Goods_no')}:</label>
                            <div class="col-xs-12 col-sm-8">
                                <input id="c-goods_no" data-rule="required" class="form-control" name="row[goods_no]" type="text" value="{$row['goods_no']?:''}" >
                            </div>
                        </div>
                    -->

                    <div class="form-group">
                        <label for="c-goods_name" class="control-label col-xs-12 col-sm-2">{:__('Goods_name')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-goods_name" data-rule="required" class="form-control" name="row[goods_name]" type="text" value="{$row['goods_name']?:''}" >
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-brand_id" class="control-label col-xs-12 col-sm-2">{:__('Brand_id')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-brand_id"  data-source="brand/index" class="form-control selectpage" name="row[brand_id]" type="text" value="{$row['brand_id']?:''}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-store_count" class="control-label col-xs-12 col-sm-2">{:__('Store_count')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-store_count" data-rule="required" class="form-control" name="row[store_count]" type="number"  value="{$row['store_count']?:'0'}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-weight" class="control-label col-xs-12 col-sm-2">{:__('Weight')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-weight" data-rule="required" class="form-control" name="row[weight]" type="number"  value="{$row['weight']?:'0'}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-market_price" class="control-label col-xs-12 col-sm-2">{:__('Market_price')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-market_price" data-rule="required" class="form-control" step="0.01" name="row[market_price]" type="number"  value="{$row['market_price']?:'0'}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-shop_price" class="control-label col-xs-12 col-sm-2">{:__('Shop_price')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-shop_price" data-rule="required" class="form-control" step="0.01" name="row[shop_price]" type="number" value="{$row['shop_price']?:'0'}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-cost_price" class="control-label col-xs-12 col-sm-2">{:__('Cost_price')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-cost_price" class="form-control" step="0.01" name="row[cost_price]" type="number" value="{$row['cost_price']?:'0'}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-keywords" class="control-label col-xs-12 col-sm-2">{:__('Keywords')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-keywords" data-rule="required" class="form-control" name="row[keywords]" type="text" value="{$row['keywords']?:''}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-goods_remark" class="control-label col-xs-12 col-sm-2">{:__('Goods_remark')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-goods_remark" data-rule="required" class="form-control" name="row[goods_remark]" type="text"  value="{$row['goods_remark']?:''}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-content" class="control-label col-xs-12 col-sm-2">{:__('Content')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <textarea id="c-content" class="form-control editor" rows="5" name="row[content]" cols="50">{$row['content']?:''}</textarea>
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-original_image" class="control-label col-xs-12 col-sm-2">主图:</label>
                        <div class="col-xs-12 col-sm-8">
                            <div class="input-group">
                                <input id="c-original_image" data-rule="required" class="form-control" size="50" name="row[original_image]" type="text" value="{$row['original_image']?:''}">
                                <div class="input-group-addon no-border no-padding">
                                    <span><button type="button" id="plupload-original_image" class="btn btn-danger plupload" data-input-id="c-original_image" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="false" data-preview-id="p-original_image"><i class="fa fa-upload"></i> {:__('Upload')}</button></span>
                                    <span><button type="button" id="fachoose-original_image" class="btn btn-primary fachoose" data-input-id="c-original_image" data-mimetype="image/*" data-multiple="false"><i class="fa fa-list"></i> {:__('Choose')}</button></span>
                                </div>
                                <span class="msg-box n-right" for="c-original_image"></span>
                            </div>
                            <ul class="row list-inline plupload-preview" id="p-original_image"></ul>
                        </div>
                    </div>

                    <!--  
                        <div class="form-group">
                            <label for="c-is_virtual" class="control-label col-xs-12 col-sm-2">{:__('Is_virtual')}:</label>
                            <div class="col-xs-12 col-sm-8">
                                            
                                <select  id="c-is_virtual" v-model="row.is_virtual" class="form-control selectpicker" name="row[is_virtual]">
                                    {foreach name="isVirtualList" item="vo"}
                                        <option value="{$key}" {in name="key" value="0"}selected{/in}>{$vo}</option>
                                    {/foreach}
                                </select>

                            </div>
                        </div>
                        <div class="form-group">
                            <label for="c-virtual_indate" class="control-label col-xs-12 col-sm-2">{:__('Virtual_indate')}:</label>
                            <div class="col-xs-12 col-sm-8">
                                <input id="c-virtual_indate" class="form-control" name="row[virtual_indate]" type="number" value="{$row['virtual_indate']?:''}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="c-virtual_limit" class="control-label col-xs-12 col-sm-2">{:__('Virtual_limit')}:</label>
                            <div class="col-xs-12 col-sm-8">
                                <input id="c-virtual_limit" class="form-control" name="row[virtual_limit]" type="number" value="{$row['virtual_limit']?:''}">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="c-virtual_refund" class="control-label col-xs-12 col-sm-2">{:__('Virtual_refund')}:</label>
                            <div class="col-xs-12 col-sm-8">
                                            
                                <select  id="c-virtual_refund" v-model="row.virtual_refund" class="form-control selectpicker" name="row[virtual_refund]">
                                    {foreach name="virtualRefundList" item="vo"}
                                        <option value="{$key}" {in name="key" value="1"}selected{/in}>{$vo}</option>
                                    {/foreach}
                                </select>

                            </div>
                        </div>
                    -->
                    <div class="form-group">
                        <label for="c-is_free_shipping" class="control-label col-xs-12 col-sm-2">{:__('Is_free_shipping')}:</label>
                        <div class="col-xs-12 col-sm-8">
                                        
                            <select  id="c-is_free_shipping" v-model="row.is_free_shipping" class="form-control selectpicker" name="row[is_free_shipping]">
                                {foreach name="isFreeShippingList" item="vo"}
                                    <option value="{$key}" {in name="key" value="0"}selected{/in}>{$vo}</option>
                                {/foreach}
                            </select>

                        </div>
                    </div>

                    <div class="form-group">
                        <label for="c-shipping_template_id" class="control-label col-xs-12 col-sm-2">运费模板:</label>
                        <div class="col-xs-12 col-sm-8">
                            <select name="row[shipping_template_id]" v-model="row.shipping_template_id" class="form-control" id="c-shipping_template_id">
                                {foreach name="shippingTemplates" id="st"}
                                    <option value="{$st.id}">{$st.name}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>

                    <div class="form-group">
                        <label for="c-sort" class="control-label col-xs-12 col-sm-2">{:__('Sort')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-sort" data-rule="required" class="form-control" name="row[sort]" type="number" value="{$row['sort']?:'50'}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-is_on_sale" class="control-label col-xs-12 col-sm-2">{:__('Is_recommend')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <label for="c-is_recommend" class="checkbox-inline">
                                <input type="checkbox" name="row[is_recommend]" id="c-is_recommend" v-model="row.is_recommend" value="1"> {:__('Is_recommend')}                                      
                            </label>

                            <label for="c-is_new" class="checkbox-inline">
                                <input type="checkbox" name="row[is_new]" id="c-is_new" v-model="row.is_new" value="1"> {:__('Is_new')}                                      
                            </label>

                            <label for="c-is_hot" class="checkbox-inline">
                                <input type="checkbox" name="row[is_hot]" id="c-is_hot" v-model="row.is_hot" value="1"> {:__('Is_hot')}                                      
                            </label>

                        </div>
                    </div>

       
                    <div class="form-group">
                        <label for="c-commission" class="control-label col-xs-12 col-sm-2">{:__('Commission')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-commission" class="form-control" step="0.01" name="row[commission]" type="number" value="{$row['commission']?:''}">
                        </div>
                    </div>
     <!--                <div class="form-group">
                        <label for="c-spu" class="control-label col-xs-12 col-sm-2">{:__('Spu')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-spu" class="form-control" name="row[spu]" type="text" value="{$row['spu']?:''}">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="c-sku" class="control-label col-xs-12 col-sm-2">{:__('Sku')}:</label>
                        <div class="col-xs-12 col-sm-8">
                            <input id="c-sku" class="form-control" name="row[sku]" type="text" value="{$row['sku']?:''}">
                        </div>
                    </div> -->
                    
                    
                </div>
                <div role="tabpanel" class="tab-pane" id="imgs">
                    <div class="form-group">
                        <label for="c-images" class="control-label col-xs-12 col-sm-2">商品相册:</label>
                        <div class="col-xs-12 col-sm-8">
                            <div class="input-group">
                                <input id="c-images" data-rule="required" class="form-control hidden" size="50" name="row[images]" type="text" value="{$row.images?:''}">
                                <div class="input-group-addon no-border no-padding">
                                    <span><button type="button" id="plupload-images" class="btn btn-danger plupload" data-input-id="c-images" data-mimetype="image/gif,image/jpeg,image/png,image/jpg,image/bmp" data-multiple="true" data-preview-id="p-images"><i class="fa fa-upload"></i> {:__('Upload')}</button></span>
                                    <span><button type="button" id="fachoose-images" class="btn btn-primary fachoose" data-input-id="c-images" data-mimetype="image/*" data-multiple="true"><i class="fa fa-list"></i> {:__('Choose')}</button></span>
                                </div>
                                <span class="msg-box n-right" for="c-images"></span>
                            </div>
                            <ul class="row list-inline plupload-preview" id="p-images"></ul>
                        </div>
                    </div>
                </div>
                <div role="tabpanel" class="tab-pane" id="spec">
                    <div class="form-group" v-for="(spec, index) in specs">      
                        <label for="" class="control-label col-xs-12 col-sm-2">{{spec.name}}:</label>      
                        <div class="col-xs-12 col-sm-8">
                            <button class="btn  spec-btn" :class="item.selected?'btn-success':'btn-default'" type="button" @click="selectSpec(item)" v-for="item in spec.items">{{item.name}}</button>
                        </div>  
                    </div>

                    <table class="table table-hover table-bordered" v-if="specInputItem">
                        <tr class="alert-success">
                            <th v-for="title in specInputItem.title">{{title}}</th>
                            <th>价格</th>
                            <th>库存</th>
                            <th>SKU</th>
                            <th>操作</th>
                        </tr>
                        <tr v-for="(items,index) in specInputItem.items">
                            <th v-for="name in items.names">{{name}}</th> 
                            <th>
                                <input type="text" class="form-control" :name="'spec['+items.key+'][price]'" :value="items.price">
                            </th>
                            <th>
                                <input type="text" class="form-control" :name="'spec['+items.key+'][store_count]'" :value="items.store_count">
                            </th>
                            <th>
                                <input type="text" class="form-control" :name="'spec['+items.key+'][sku]'" :value="items.sku">
                            </th>
                            <input type="hidden" :name="'spec['+items.key+'][spec_name]'" :value="items.spec_name">
                            <th>
                                <button class="btn btn-xs btn-danger btn-delone" type="button" @click="delInputItem(index, items.id)">
                                    <i class="fa fa-trash"></i>
                                </button>
                            </th>
                        </tr>
                    </table>
                </div>
                <div role="tabpanel" class="tab-pane" id="attribute">

                    <div class="form-group" v-for="(attr, index) in attributes">      
                        <label for="" class="control-label col-xs-12 col-sm-2">{{attr.name}}:</label>      
                        <div class="col-xs-12 col-sm-8">
                            <label class="radio-inline" v-if="attr.type == 1" :for="'row_attr_'+index+'_'+k" v-for="(v,k) in attr.values">
                                <input type="radio" :checked="v.checked?'checked':''"  :name="'row[attr]['+attr.id+']'" :value="v.value" @click="v.checked = !v.checked" :id="'row_attr_'+index+'_'+k">{{v.value}}
                            </label>
                           <input id="" v-if="attr.type == 3" class="form-control" :name="'row[attr]['+attr.id+']'"  type="text" :value="attr.values">

                           <label class="checkbox-inline" v-if="attr.type == 2" :for="'row_attr_'+index+'_'+k" v-for="(v,k) in attr.values">
                                <input type="checkbox" :checked="v.checked?'checked':''"  :name="'row[attr]['+attr.id+'][]'" :value="v.value" :id="'row_attr_'+index+'_'+k">{{v.value}}
                            </label>

                        </div>  
                    </div>

                </div>
            </div>
        
            <div class="form-group">
                <label class="control-label col-xs-12 col-sm-2"></label>
                <div class="col-xs-12 col-sm-8">
                    <button type="submit" class="btn btn-success btn-embossed">{:__('OK')}</button>
                    <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script src="https://unpkg.com/vue@2.5.16/dist/vue.js"></script>
<script src="/assets/js/vue/vue-resource.js"></script>
<script>

    var addVue = new Vue({
        el:'.add-form',
        data: {
            specs: [],
            attributes: [],
            category_id: {$row.category_id?:0},
            selectedSpecs: [],
            specInputItem:[],
            row: {$row?:''},
            goodsId: {:input('ids')?:'0'},
        },
        mounted:function(){
            _this = this;
            var cid = _this.category_id;
            if(_this.goodsId > 0){
                this.getAttr(cid);
                this.getSpec(cid);
            }
        },
        methods: {
            getAttr: function(cid){
                _this = this;
                this.$http.post('{:url("goodsattribute/getCategoryAttr")}',{  
                    cid: cid,
                    goods_id: _this.goodsId,
                },{  
                    emulateJSON:true  
                }).then(function(res){  
                    if(res.data.code){
                        _this.attributes = res.data.data;
                    }                             
                },function(error){  
                    layer.msg('查询分类属性出错');
                });
                    
            },
            getSpec: function(cid){
                _this = this;
                this.$http.post('{:url("spec/getCategorySpec")}',{  
                    cid: cid,
                    goods_id: _this.goodsId,
                },{  
                    emulateJSON:true  
                }).then(function(res){  
                    if(res.data.code){
                        _this.specs = res.data.data;
                        _this.changeSelectSpec();
                    }                             
                },function(error){  
                    layer.msg('查询分类规格出错');
                });
                    
            },

            //选中
            selectSpec: function(item){
                console.log(item);
                if(typeof(item.selected) == 'undefine'){
                    this.set(item,'selected',true);
                } else {
                    item.selected = !item.selected;
                }


                this.changeSelectSpec();
            },
            //检查选中的选项值
            changeSelectSpec: function(){
                _this = this;
                var selectedSpecs = [];
                _this.specs.forEach(function(spec){
                    spec.items.forEach(function(item,index){
                        if(item.selected){
                            selectedSpecs.push(item.id);
                        }
                    })
                })
                this.selectedSpecs = selectedSpecs;
                console.log(this.selectedSpecs);
                this.getSpecInput();
            },
            //获取规格输入
            getSpecInput: function(){
                var ids = this.selectedSpecs.toString();
                _this = this;
                $.ajax({
                    url: '{:url("spec/getSpecInput")}',
                    type: 'post',
                    dataType: 'json',
                    data: {
                        ids: ids,
                        goods_id: _this.goodsId,
                    },
                    success:function(res){
                        if(res.code){
                            _this.specInputItem = res.data;
                        }                         
                    }
                })
                .fail(function() {
                    layer.msg('获取规格输入出错');
                });
            },
            //删除某行规格输入框
            delInputItem: function(index,id){
                //如果已经存在的，删除数据库的数据
                if(id){
                    _this = this;
                    this.$http.post('{:url("spec/delSpecPrice")}',{  
                        id: id,
                    },{  
                        emulateJSON:true  
                    }).then(function(res){  
                        if(res.data.code){
                            _this.specInputItem.items.splice(index,1);
                        } else{
                            layer.msg('删除规格价格出错');
                        }                          
                    },function(error){  
                        layer.msg('删除规格价格出错');
                    });
                } else {
                    this.specInputItem.items.splice(index,1);
                }
            }

        },
        watch: {
            category_id(newVal, oldVal){
                if(newVal){
                    this.getAttr(newVal);
                    this.getSpec(newVal);
                } else {
                    this.specs = [];
                    this.attributes = [];
                }
            }
        }
    });




</script>
